home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / noweb / contrib / partingr / mm2mx65 < prev    next >
Text File  |  1995-02-24  |  5KB  |  180 lines

  1. #!/usr/common/bin/perl
  2. do "getopts.pl" || die "$!";
  3. do Getopts('imnd:l:');
  4.  
  5. do "TeXthings";
  6.  
  7. $sectionref='aaa'; $i=0;
  8. if($opt_l)
  9.  { open(LOGFILE,">$opt_l") || die "$!"; }
  10.   
  11. while(<>)
  12. {
  13. #  s/\n$//;
  14.   s/^@//;
  15.   if(/@</)
  16.    { ($pre,$mid,$end)=&convxrefs($_);
  17.       @lines[$i]=$pre; $i++;
  18.       $_=$mid; # need to expand the module name here
  19.    }
  20. # expand wildcard references here, then process as normal
  21.   if(/^(use|defn) (.*)\.{3}$/)
  22.     { print LOGFILE "Wildcard `$2...', expands to  " if $opt_l;
  23.       @matches=grep(/^$2.*/i,split(/ñ/,$known));
  24.       if($#matches>0)
  25.         { 
  26.           print LOGFILE "[",join('][',@matches),"] " if $opt_l;
  27.           print STDERR "Ambiguous module name `$mod...', line $i\n";
  28.           print STDERR "Matches: [",join('][',@matches),"]\n";
  29.           print STDERR "Using `",@matches[0],"'\n";
  30.           #die "\nAmbiguous module name `$mod...', line $i"; 
  31.         }
  32.       elsif($#matches==-1)
  33.         { 
  34.           die "\nNo match for name `$mod...', line $i";
  35.         }
  36.       $mn=@matches[0];
  37.       if($mn=~/\[\[/) { $mn=&convquotes($mn); }
  38.       print LOGFILE $mn,"\n" if $opt_l;
  39.       $_="$1 $mn";
  40.     }
  41.  
  42. # process a <<defn>>=
  43.   if(/^defn (.*)$/)
  44.     { print LOGFILE "Defining chunk `$1' with macro $sectionref\n" if $opt_l;
  45.       $md=$1; $mn=$1; if($opt_n) { $md=~tr/A-Z/a-z/; }
  46.       $mt=$md;
  47.       $mt=~s/([*+.?{}()])/\\\1/g;
  48.       if($known!~/ñ$mt/) { $known=$known . "ñ$md"; }
  49.       @names{$md}=1; $currentmod=$md;
  50.       @defines{$md}=@defines{$md} . "\\\\{\\xp\\$sectionref}";
  51.       #if($mn=~/\[\[/) { $mn=&convquotes($mn); }
  52.       @lines[$i]="defn $sectionref $mn\n"; $oldref=$sectionref;
  53.       $sectionref++;
  54.       push(@uses,$i);
  55.       $indxing=0;
  56.     }
  57. # process a <<use>>
  58.   elsif(/^use (.*)$/)
  59.     { print LOGFILE "Using chunk `$1' in chunk $oldref\n" if $opt_l;
  60.       $md=$1; $mn=$1; if($opt_n) { $md=~tr/A-Z/a-z/; }
  61.       $mt=$md;
  62.       $mt=~s/([*+.?{}()])/\\\1/g;
  63.       if($known!~/ñ$mt/) { $known=$known . "ñ$md"; }
  64.       @reference{$md}=@reference{$md} . "\\\\{\\xp\\$oldref}";
  65.       #if($mn=~/\[\[/) { $mn=&convquotes($mn); }
  66.       @lines[$i]="use $oldref $mn\n";
  67.       push(@uses,$i); 
  68.       $indxing=0;
  69.     }
  70. # process identifier information
  71.   elsif(/^index (nl|defn |use )(.*)/)
  72.     {  
  73.       if($2 eq '|') { $indxing=1; }
  74.       else
  75.       { if($2)
  76.         { print LOGFILE "Identifier `$2' indexed as " if $opt_l;
  77.           if($1 eq "defn " && !$indxing)
  78.            { print LOGFILE "defined [$oldref]\n" if $opt_l;
  79.              $style="";
  80.            }
  81.           else
  82.            { print LOGFILE "used [$oldref]\n" if $opt_l;
  83.              $style="\\it";
  84.            }
  85.           @variables{$2}=@variables{$2} . ",\\thinspace{$style\\xp\\$oldref}";
  86.         }
  87.       }
  88.       $i--; # don't put this line in the file here
  89.     }
  90. # stick the line in the array
  91.   else
  92.    { @lines[$i]=$_; }
  93.   $i++;
  94.   if($end) { @lines[$i]=$end; $i++; undef $end; }
  95. }
  96.  
  97. if($opt_l)
  98.  {
  99.    print LOGFILE "\n\nList of modules currently defined\n";
  100.    print LOGFILE join("\n",sort keys(defines));
  101.    print LOGFILE "\n\nList of modules currently referenced\n";
  102.    print LOGFILE join("\n",sort keys(reference));
  103.    print LOGFILE "\n\n";
  104.  }
  105.  
  106. foreach(keys(reference))
  107.  { @longlist{$_}=@reference{$_}; }
  108.  
  109. foreach(keys(defines))
  110.  { @longlist{$_}=@longlist{$_} . ('%' . @defines{$_}); }
  111.  
  112. foreach(@uses)
  113.  { 
  114.    $ref=@lines[$_];
  115.    $ref=~/^(use|defn) (...) (.*)/;
  116.    $mn=$3; $dr=$2; $ac=$1;
  117.    $defns=@defines{$mn};
  118.    print LOGFILE "Module $mn " if $opt_l;
  119.    if($ac eq 'defn')
  120.     { $uses=@reference{$mn}; $uses="{$uses}|";
  121.       print LOGFILE "defined at line $_, $uses\n" if $opt_l;
  122.     }
  123.    else
  124.     { $uses=''; 
  125.       print LOGFILE "referenced at line $_\n" if $opt_l;
  126.     }
  127.    print LOGFILE "Line $_ modified to `$ac $dr {$defns} $uses$mn'\n" if $opt_l;
  128.    $mn=&convquotes($mn);
  129.    @lines[$_]="$ac $dr|{$defns}|$uses $mn\n";
  130.  }
  131.         
  132. print STDOUT "header tex \n",@lines;
  133.  
  134. # now @longlist{MOD} contains a list of all the references to <MOD>
  135. # sort them to make them look pretty
  136.  
  137. if($opt_m)
  138.   {
  139.     print LOGFILE "Making module index...\n" if $opt_l;
  140.     print "index mods\n" if $opt_l;
  141.     foreach(sort keys(longlist))
  142.       {
  143.         $defns=@defines{$_};
  144.         $defns=~s/^,\\\\thinspace//;
  145.         print LOGFILE "Module <$_ $defns> ",@reference{$_},"\n" if $opt_l;
  146.        # first we print the module name and defining numbers
  147.         print "entry {\\LA ",&convquotes($_),"\\ \\xwp{$defns}\\RA}\\quad";
  148.        # now we print the bit after that : assume foot=cmr8
  149.         print "{\\foot\\xtc{",@reference{$_},"}}\n";
  150.       }
  151.     print "end index\n";
  152.   }
  153.  
  154. if($opt_i)
  155.   {
  156.     print "index ids\n";
  157.     foreach(sort keys(variables))
  158.      {
  159.         $vars=@variables{$_};
  160.         $vars=~s/^,\\thinspace//;
  161.         print "entry {\\code ",&TeXliteral($_),"\\edoc} :\\quad",$vars,"\n";
  162.       }
  163.     print "end index\n";
  164.   }
  165.   
  166. print STDOUT "trailer tex\n";
  167.  
  168. sub convxrefs
  169.  {
  170.    local($l)=@_;
  171.    local($found,$output);
  172.    $found=index($l,'@<');
  173.    $lost=index($l,'@>',$found);
  174.    $pre=substr($l,0,$found) . "\n"; # before the use
  175.    $mid="use " . substr($l,$found+2,$lost-$found-2) . "\n";
  176.    $end="text " . substr($l,$lost+2);
  177.    substr($l,$found,$lost+2-$found)="";
  178.    return $pre,$mid,$end;
  179.  }
  180.